<?php
class IndexAction extends CommonAction {
	
	// 框架首页
	public function index() {
		if (isset ( $_SESSION [C ( 'USER_AUTH_KEY' )] )) {

			/*
			//显示菜单项
			$menu = array ();
			
			//读取数据库模块列表生成菜单项
			$node = M ( "Node" );
			$id = $node->getField ( "id" );
			$where ['level'] = 2;
			$where ['status'] = 1;
			$where ['pid'] = $id;
			$list = $node->where ( $where )->field ( 'id,name,group_id,title' )->order ( 'sort asc' )->select ();
			$accessList = $_SESSION ['_ACCESS_LIST'];
			foreach ( $list as $key => $module ) {
				if (isset ( $accessList [strtoupper ( APP_NAME )] [strtoupper ( $module ['name'] )] ) || $_SESSION ['administrator']) {
					//设置模块访问权限
					$module ['access'] = 1;
					$menu [$key] = $module;
				}
			}
			
			if (! empty ( $_GET ['tag'] )) {
				$this->assign ( 'menuTag', $_GET ['tag'] );
			}
			//dump($menu);
			$this->assign ( 'menu', $menu );
			*/
            //显示菜单项
            $menu  = array();
                //读取数据库模块列表生成菜单项
                $node    =   M("Node");
                $id	=	$node->getField("id"); //只取排序后第一个记录字段值
               // $where['level']=2;
                $where['status']=1;
                //$where['pid']=$id;
                $list	=	$node->where($where)->field('id,name,group_id,title,level,pid')->order('sort asc')->select();
				//dump( $list);
                if(isset($_SESSION['_ACCESS_LIST'])) {
                    $accessList = $_SESSION['_ACCESS_LIST'];
                }else{
                    //import('@.ORG.Util.RBAC');
                    import('ORG.Util.RBAC');
                    $accessList =   RBAC::getAccessList($_SESSION[C('USER_AUTH_KEY')]);
                }
				//dump($accessList);
                foreach($list as $key=>$module) {
					//$x1=APP_NAME;
					//$x2=$module['name'];
					//$x3=$module['id'];
					//echo "$x1 == $x2 == $x3 <br />";
					if ($module['level']==2){
						 if(isset($accessList[strtoupper(APP_NAME)][strtoupper($module['name'])]) || $_SESSION['administrator']) {
							//设置模块访问权限
							$module['access'] =   1;
							$menu[$key]  = $module;
						}
					}

					if ($module['level']==3){
						 //if(isset($accessList[strtoupper(APP_NAME)][strtoupper($module['name'])]) || $_SESSION['administrator']) {
						 if($_SESSION['administrator']) {
							//设置模块访问权限
							$module['access'] =   1;
							$menu[$key]  = $module;
						}
						else {
							//if (in_array($module['id'],$accessList)){
							foreach($accessList as $key3=>$module3) {
								foreach($module3 as $key4=>$module4) {
									foreach($module4 as $key5=>$module5) {
										if ($module['id']==$module5){
										//dump($module5);
										//设置模块访问权限
										$module['access'] =   1;
										$menu[$key]  = $module;
										}
									}
								}
							}
						}
					}
                }

                //缓存菜单访问
                $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')]]	=	$menu;
            //}
            if(!empty($_GET['tag'])){
                $this->assign('menuTag',$_GET['tag']);
            }
            //dump($menu);
			$list2=array();
            foreach($menu as $key=>$module) {
				if ($module[level]==2 and $module[pid]==$id){
					$list2[$key]=$module;
				}
			}
			$list3=array();
			$i=0;
            foreach($list2 as $key2=>$module2) {
				$x2=$module2[id];
				//$list3[$i]=$module2;
				//$i++;
				$list2[$key2][level3]=0; //初始无下设3级菜单
				foreach($menu as $key=>$module) {
					if ($module[level]==3 and $module[pid]==$x2){
						$list3[$i]=$module;
						$list2[$key2][level3]=1; //有下设3级菜单
						$i++;
					}
				}
			}
			//dump($list2);
			//exit;
            //$this->assign('menu',$menu);
            //$this->assign('menu',$list2);

            $this->assign('menu2',$list2);
            $this->assign('menu',$list3);


 			if ($_SESSION['administrator']){
				unset($list);
				$model	=	M("Group"); // 取横向导航菜单
				$list	=	$model->where("status=1")->getField('id,title');
			}
			else {
				foreach($menu2 as $key=>$module) {
					$x1=$module[group_id];
					$group_list[$x1]=$module[group_id];
				}
				$x1=implode ( "," ,$group_list );
				unset($list);
				$model	=	M("Group"); // 取横向导航菜单
				$list	=	$model->where("status=1 and id in ($x1)")->getField('id,title');
			}
			//dump($list);
			//exit;
			unset($list2);
			$i=0;
			foreach($list as $key=>$module) {
				$list2[$i][gid]=$key;
				$list2[$i][title]=$module;
				$i++;

			}
			$this->assign('nodeGroupList',$list2); //值是组名，下标是group_id值

		}
		C ( 'SHOW_RUN_TIME', false ); // 运行时间显示
		C ( 'SHOW_PAGE_TRACE', false );
		$this->display();
	}

}
?>